import {message} from 'antd';
import React, {useRef} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import {isSuccess} from "@/utils/utils";
import {UserManager} from "@/services/api-functions";
import ProTable, {ActionType, ProColumns} from "@ant-design/pro-table";
import {SYS_ENUMS} from "@/utils/constants";

export const userStateChange = async (select: any, state: number) => {
  if (!select) return true;
  try {
    if (isSuccess((await UserManager.userEnablingMod({userId: select.id, enabling: state}))?.code)) {
      message.success(state ? '解除成功' : '冻结成功');
      return true;
    }
    return false
  } catch (error) {
    return false;
  }
};


const MemberList: React.FC<any> = ({}) => {
  const actionRef = useRef<ActionType>();
  const columns: ProColumns[] = [
    {
      title: 'ID',
      dataIndex: ['answer','id'],
      ellipsis: true,
      width: 120,
    },
    {
      title: '科目',
      dataIndex: ['detail', 'subjectId'],
      valueEnum: SYS_ENUMS.SUBJECT_ENUMS,
      ellipsis: true,
      width: 120,
    },
    {
      title: '难度等级',
      dataIndex: ['detail', 'hardLevel'],
      ellipsis: true,
      valueEnum: SYS_ENUMS.HARD_ENUMS,
      width: 120,
    },
    {
      title: '题目标题',
      dataIndex: ['detail', 'title'],
      ellipsis: true,
      width: 120,
    },
    {
      title: '作题答案',
      dataIndex: ['answer','answerValue'],
      ellipsis: true,
      width: 120,
    },
    {
      title: '批改结果',
      dataIndex: ['answer','correctResult'],
      valueEnum: SYS_ENUMS.CORRECT_RESULT,
      ellipsis: true,
      width: 120,
    },
    {
      title: '答题人学号',
      dataIndex: ['user','stuNo'],
      ellipsis: true,
      width: 120,
    },
    {
      title: '答题人姓名',
      dataIndex: ['user','name'],
      ellipsis: true,
      width: 120,
    },
    {
      title: '答题人昵称',
      dataIndex: ['user','nickName'],
      ellipsis: true,
      width: 120,
    },
    {
      title: '答题时间',
      dataIndex: ['answer','createTime'],
      ellipsis: true,
      width: 120,
    },
  ];

  return (
    <PageContainer>
      <ProTable<any, any>
        actionRef={actionRef}
        rowKey="key"
        search={false}
        request={async (params) => {
          const query = {...params}
          const {data: {records, total}, code}: any = await UserManager.queryAnswerList(query);
          return {data: records || [], success: isSuccess(code), total: isSuccess(code) ? total : 0};
        }}
        columns={columns}
        scroll={{ x: 1300 }}
      />
    </PageContainer>
  );
};

export default MemberList;
